package com.example.crm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.crm.dao.CustomerDAO;
import com.example.crm.pojo.Customer;

@WebServlet("/editcustomer")
public class EditCustomerServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
				
		// 1. 获取参数
		String idStr = req.getParameter("id");
		int id = Integer.parseInt(idStr);
				
		// 2. 调用model
		CustomerDAO dao = new CustomerDAO();
		Customer c = null;
		try {
			c = dao.findById(id);
			
			req.setAttribute("customer", c);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		// 3. 请求转发
		req.getRequestDispatcher("jsp/customer/edit.jsp").forward(req, resp);
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
